package com.snl.micro.blog.user.service.impl;

import com.snl.micro.blog.common.action.UserAction;
import com.snl.micro.blog.common.base.schuleThreadPool.CommonPool;
import com.snl.micro.blog.common.param.UserSaveParam;
import com.snl.micro.blog.common.protocol.ResponseResult;
import com.snl.micro.blog.common.query.ArticleQueryParam;
import com.snl.micro.blog.common.query.LoginParam;
import com.snl.micro.blog.common.query.UserQuery;
import com.snl.micro.blog.common.vo.ArticleVo;
import com.snl.micro.blog.user.base.ThreadPool.ThreadTask;
import com.snl.micro.blog.user.client.ArticleClient;
import com.snl.micro.blog.user.dao.repository.UserRepository;
import com.snl.micro.blog.user.pojo.po.UserInfoPo;
import com.snl.micro.blog.user.pojo.vo.UserVo;
import com.snl.micro.blog.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserServiceImplement implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ArticleClient articleClient;

    @Autowired
    private ThreadTask threadTask;

    @Override
    public void saveUser(UserSaveParam param) {
        log.debug("用户业务入参，{}",param);
        UserInfoPo po = new UserInfoPo();
        BeanUtils.copyProperties(param,po);
        po.setCreateTime(LocalDateTime.now());
        LocalDateTime now = LocalDateTime.now();
        po.setAge(now.getYear() - po.getBirthDay().getYear());

        userRepository.saveUser(po);
    }

    @Override
    public UserInfoPo login(LoginParam param) {
        log.debug("用户业务层参数:{}",param);
        UserInfoPo po = userRepository.login(param);
        if (Objects.isNull(po)) {
            throw new RuntimeException("用户不存在，请注册");
        }
        UserAction userAction = new UserAction();
        userAction.setCreateTime(LocalDateTime.now());
        userAction.setUsername(po.getUsername());
        userAction.setMessage("登陆成功");
        CommonPool.addAction(userAction);
        return po;
    }

    @Override
    public List<UserVo> query(UserQuery query) {
        List<UserInfoPo> pos = userRepository.query(query);
        List<UserVo> vos = new ArrayList<>();
        pos.forEach(po->{
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(po,userVo);
            //设置粉丝和关注人
            //获取当前用户id
            Long id = po.getId();
            //获取当前用户的粉丝和关注者
            HashMap<String, List<UserInfoPo>> fansAndFollowings =
                    this.threadTask.getFansAndFollowings(id);
            userVo.setFans(fansAndFollowings.get("fans"));
            userVo.setFollowings(fansAndFollowings.get("followings"));
            vos.add(userVo);
        });
        return vos;
    }

    @Override
    public void subscrible(Long followerId, Long followingId) {
        userRepository.subscrible(followerId,followingId);
    }

    @Override
    public void unsubscrible(Long followerId, Long followingId) {
        userRepository.unsubscrible(followerId,followingId);
    }


}
